<template>
  <div id="app">
    <a-input :value="inputVal" @change="setInputChange" placeholder="请输入任务" class="my_ipt" />
    <a-button type="primary" @click="addItem">添加事项</a-button>

    <a-list bordered :dataSource="btnStatusShow" class="dt_list">
      <!-- :dataSource="list" -->
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @change="checkboxChange($event,item.id)">{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="removeItem(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{getNum}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button @click="changeBtnStatus('all')" :type="btnStatus==='all'?'primary':'default'">全部</a-button>
          <a-button
            @click="changeBtnStatus('udone')"
            :type="btnStatus==='udone'?'primary':'default'"
          >未完成</a-button>
          <a-button
            @click="changeBtnStatus('done')"
            :type="btnStatus==='done'?'primary':'default'"
          >已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a>清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
export default {
  name: 'app',
  data() {
    return {}
  },
  methods: {
    // 当输入框输入数据时
    setInputChange(e) {
      this.$store.commit('setInputValue', e.target.value)
    },

    // 点击按钮添加操作时
    addItem() {
      // 注意在添加时先判断先去除两端空格
      if (this.inputVal.trim().length === 0) {
        return this.$message.warning('请先输入字段')
      }
      this.$store.commit('addItemHandle')
    },

    // 点击删除时
    removeItem(id) {
      this.$store.commit('removeItemHandle', id)
    },

    // 当点击复选框时
    checkboxChange(e, id) {
      const params = {
        id,
        done: e.target.checked
      }
      this.$store.commit('getCheckbox', params)
    },

    // 当操作按钮发生切换时
    changeBtnStatus(params) {
      this.$store.commit('btnStatusChange', params)
    }
  },
  computed: {
    ...mapState(['inputVal', 'btnStatus']),
    ...mapGetters(['getNum', 'btnStatusShow'])
  },
  created() {
    this.$store.dispatch('getList')
  }
}
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
